local hx__sporjbianshen = fk.CreateSkill {
  name = "hx__sporjbianshen",
}


hx__sporjbianshen:addEffect("active", {
  prompt = "#hx__sporjbianshen",
  can_use = function(self, player)
    local hp = player.hp
    local hand = player:getHandcardNum()
    local skills = #player:getSkillNameList()
    local max = math.max(hp, hand, skills)
    return player:getMark("@hx__sporj") >= max
  end,
  card_num = 0,
  target_num = 0,
  on_use = function(self, room, effect)
    local player = effect.from
    local hp = player.hp
    local hand = player:getHandcardNum()
    local skills = #player:getSkillNameList()
    local max = math.max(hp, hand, skills)
    local choice = room:askToChoice(player, {
      choices = {"hx__sporjbianshen-recover", "hx__sporjbianshen-draw","hx__sporjbianshen-skill:::" .. max},
      cancelable = false,
      skill_name = hx__sporjbianshen.name,
    })
    if choice == "hx__sporjbianshen-recover" then
      room:recover{ who = player, num = 1, recoverBy = player, skillName = hx__sporjbianshen.name }
      room:changeMaxHp(player, 1)
    elseif choice == "hx__sporjbianshen-draw" then
      player:drawCards(player:getMaxCards() - player:getHandcardNum(), hx__sporjbianshen.name)
      room:addPlayerMark(player, "@hx__sporjbianshen", 1)
    else
      local allSkills = {}
      local cannot = {}
      table.insertIfNeed(cannot, Fk.generals["hxdoro__spdoro"])
      table.insertIfNeed(cannot, Fk.generals[player.general])
      if player.deputyGeneral then
        table.insertIfNeed(cannot, Fk.generals[player.deputyGeneral])
      end
      for _, general in pairs(Fk:getAllGenerals(cannot)) do
        for _, skillName in ipairs(general:getSkillNameList()) do
          local s = Fk.skills[skillName]
          if not skillName:startsWith("#") and not skillName:endsWith("&") and skillName ~= self.name
          and not s:isEquipmentSkill()  and not s.cardSkill and s:isPlayerSkill()
          then
            table.insert(allSkills, skillName)
          end
        end
      end
      local Skills = table.random(allSkills, max)
      local chosen = room:askToChoice(player, {
        choices = Skills,
        cancelable = false,
        skill_name = hx__sporjbianshen.name,
        prompt = "请选择一个技能获得",
        detailed = true,
      })
      if chosen then
        room:handleAddLoseSkills(player, chosen, nil)
        room:sendLog{
          type = "#hx__sporjbianshen_log",
          from = player.id,
          arg = chosen,
        }
      end
    end
    room:removePlayerMark(player, "@hx__sporj", max)
  end,
})

hx__sporjbianshen:addEffect("maxcards", {
  correct_func = function(self, player)
    if player:hasSkill(hx__sporjbianshen.name) then
      return player:getMark("@hx__sporjbianshen")
    end
    return 0
  end,
})

return hx__sporjbianshen
